package cn.net.isir.controller.miniprogram;

import cn.net.isir.common.RequestObject;
import cn.net.isir.utils.RequestUtils;
import cn.net.isir.common.ResultObject;
import cn.net.isir.service2.OrderGoodsService;
import cn.net.isir.service2.OrderService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuxubo
 * @filename OrderController.java
 * @package cn.net.isir.controller.miniprogram
 * @email lxb@isir.net.cn
 * @date 2018/11/12
 * @comment
 * @since 1.8
 */
@RestController("minipOrderController")
@RequestMapping(value = "/minip/order")
@Transactional
public class OrderController {
    // 声明log4j日志
    private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(OrderController.class);
    @Resource
    private OrderService orderService;
    @Resource
    private OrderGoodsService orderGoodsService;

    @RequestMapping(method = RequestMethod.GET, value = "/myrecent")
    public ResultObject myRecentOrder(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid", "userid"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        String client = String.valueOf(reqParamMap.get("client"));
        String userid = String.valueOf(reqParamMap.get("userid"));
        return orderService.orderHistory(userid, client, shopid, 3);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/myhistory")
    public ResultObject myHistory(RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid", "userid"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        String client = String.valueOf(reqParamMap.get("client"));
        String userid = String.valueOf(reqParamMap.get("userid"));
        return orderService.orderHistory(userid, client, shopid, null);
    }


    @RequestMapping(method = RequestMethod.POST, value = "/order")
    public ResultObject order(@RequestBody RequestObject obj, HttpServletRequest request, HttpServletResponse response) {
        logger.info(request.getParameter("data"));
        //声明返回值
        ResultObject ro;
        //声明参数解码后的储存变量
        Map<String, Object> reqParamMap = new HashMap<>();
        //需要检查的字段名
        String[] columns = new String[]{"shopid", "tableno", "userid", "ediblemethod", "goodslist","formid"};
        //检查失败则返回缺失的字段提示
        if ((ro = RequestUtils.parametersCheck(obj, reqParamMap, columns)).getResult() != 200) {
            return ro;
        }
        String edibleMethod = String.valueOf(reqParamMap.get("ediblemethod"));
        String shopid = String.valueOf(reqParamMap.get("shopid"));
        String tableno = String.valueOf(reqParamMap.get("tableno"));
        String userid = String.valueOf(reqParamMap.get("userid"));
        String client = String.valueOf(reqParamMap.get("client"));
        String formid = String.valueOf(reqParamMap.get("formid"));
        logger.info(formid);
        String tastes = null;
        if (reqParamMap.get("tastes") != null) {
            tastes = String.valueOf(reqParamMap.get("tastes"));
        }
        String goodsList = String.valueOf(reqParamMap.get("goodslist"));
        return orderService.order(edibleMethod, shopid, tableno, userid, client, tastes, goodsList,formid);
    }


}
